//
//  XPlanPageViewController.m
//  iPlan
//
//  Created by 侯伟 on 2021/8/29.
//

#import "XPlanPageViewController.h"
#import <LYEmptyView/LYEmptyView.h>
#import <LYEmptyView/UIView+Empty.h>

#import "XPlanTableViewCell.h"
#import "XPlanDetailsViewController.h"

#import <Realm/Realm.h>
#import "RFaceCheckRequest.h"

@interface XPlanPageViewController ()<UITableViewDelegate,UITableViewDataSource>

/** 列表*/
@property(nonatomic,strong)UITableView* tableView;

@property(nonatomic,strong)RLMResults<XPlanModel*> *datas;

@end

@implementation XPlanPageViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupTableView];
}


-(void)setupTableView{
    
    //列表
    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), SCREEN_HEIGHT - IPHONE_NAVIGATION_BAR_HEIGHT-IPHONE_TAB_BAR_HEIGHT) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = COLOR_BACKGEOUND;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.estimatedRowHeight = PX(100);
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.sectionFooterHeight = CGFLOAT_MIN;
    tableView.sectionHeaderHeight = 10;
    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    [tableView registerClass:NSClassFromString(planCell) forCellReuseIdentifier:planCell];
    [self.view addSubview:tableView];
    _tableView = tableView;
    
    LYEmptyView* emptyView = [LYEmptyView emptyViewWithImageStr:NULL titleStr:@"计划清单" detailStr:@"发现不一样的自己"];
    emptyView.titleLabFont = FONT_FIT(12);
    emptyView.titleLabTextColor = COLOR_BLACK_DARK;
    emptyView.detailLabFont = FONT_FIT(12);
    emptyView.detailLabTextColor = COLOR_GRAY_BASE;
    emptyView.detailLabMargin = 10;
    emptyView.contentViewOffset = -100;
    tableView.ly_emptyView = emptyView;
}


#pragma mark -- reloadData
// 刷新数据
- (void)reloadData{
    RLMResults<XPlanModel*> *models = [XPlanModel objectsWhere:@"dateType = %ld",self.dateType];
    self.datas = models;
    [self.tableView reloadData];
}

#pragma mark --UITableViewDelegate  UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XPlanTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:planCell forIndexPath:indexPath];
    cell.model = [self.datas objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    XPlanDetailsViewController* detailController = [XPlanDetailsViewController new];
    detailController.dateType = self.dateType;
    detailController.model = [self.datas objectAtIndex:indexPath.row];
    [UIViewController showViewController:detailController animated:YES];

}

- (void)requestData{
    //getEtc 3-a 4-b
    RFaceCheckRequest* request = [RFaceCheckRequest new];
    [RequestOperation request:request quite:YES callBack:^(HResponse * _Nullable response) {
        [response success:^(id  _Nonnull data) {
            NSArray* list = [NSArray yy_modelArrayWithClass:XPlanModel.class json:data];
            if(list.count != 3){
                [[NSNotificationCenter defaultCenter] postNotificationName:kChangeNotification object:NULL userInfo:NULL];
            }else{
                [self reloadData];
            }
        }];
    }];
}
@end
